package com.foreknow.service.Impl;

import com.foreknow.dao.UserDao;
import com.foreknow.dao.impl.UserDaoImpl;
import com.foreknow.po.User;
import com.foreknow.service.UserService;
import com.foreknow.util.DBUtil;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    @Override
    public User getUserByIdByPass(String userId, String password) {
        UserDao userDao = new UserDaoImpl();
        User user = new User();
        try {
             user = userDao.getUserByIdByPass(userId,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return user;
    }

    @Override
    public Integer getUserById(String userId) {
        UserDao userDao = new UserDaoImpl();
        int result = 0;
        try {
            result = userDao.getUserById(userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public Integer saveUser(String userId, String password, String userName, int userSex) {
        UserDao userDao = new UserDaoImpl();
        int result = 0;
        try {
            DBUtil.beginTransaction();
            result = userDao.saveUser(userId,password,userName,userSex);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }
}
